package com.arunsawad.ilertu.activity;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.arunsawad.ilertu.R;
import com.arunsawad.ilertu.common.AsyncActivity;
import com.arunsawad.ilertu.common.Constants;
import com.arunsawad.ilertu.common.RequestTask;
import com.arunsawad.ilertu.common.Utils;

public class ForgotPassword extends Activity implements AsyncActivity {
	AlertDialog.Builder alertDialogBuilder;
	SharedPreferences preferences;
	AlertDialog alert;
	EditText txtEmail;
	EditText txtIdCard;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.act_forgotpassword);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.title0_imgbtn_txt);
		
		initTitle();
		
		preferences = getSharedPreferences(Constants.PREF_FILE, MODE_PRIVATE);
	}
	
	public void initTitle() {
		ImageButton imgBtn1 = (ImageButton) findViewById(R.id.title0_imgBtn1);
		imgBtn1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), Home.class);
				startActivity(intent);
			}
		});

		TextView txtTitle = (TextView) findViewById(R.id.title0_txtTitle);
		txtTitle.setText(getString(R.string.forgot_password));
	}

	// {{ onClick events
	public void resetPassword(View view) {
		txtEmail = (EditText) findViewById(R.id.forgotpassword_txtEmail);
		txtIdCard = (EditText) findViewById(R.id.forgotpassword_txtIdCard);
		String email = txtEmail.getText().toString();
		String idCard = txtIdCard.getText().toString();
		if (email.equals("") || idCard.equals("")) {
			Utils.alert(ForgotPassword.this,
					getString(R.string.require_all_fields));
		} else {
			String msg = "";
			msg += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
			msg += "<lertParams type=\"ilertu.user-password-reset\">";
			msg += "	<common>";
			msg += "		<device>";
			msg += "			<deviceType>" + Constants.DEVICE_TYPE + "</deviceType>";
			msg += "			<deviceId>" + Constants.DEVICE_ID + "</deviceId>";
			msg += "			<currentVersion>" + Constants.DEVICE_VERSION
					+ "</currentVersion>";
			msg += "		</device>";
			msg += "		<authen/>";
			msg += "		<lastedUpdateDate/>";
			msg += "		<language>"
					+ preferences.getString(Constants.PREF_LANGUAGE, "en")
					+ "</language>";
			msg += "	</common>";
			msg += "	<user>";
			msg += "		<authen>";
			msg += "			<email>" + email + "</email>";
			msg += "		</authen>";
			msg += "		<idCard>" + idCard + "</idCard>";
			msg += "	</user>";
			msg += "</lertParams>";

			RequestTask task = new RequestTask();
			task.setListener(this);
			task.execute(Constants.URL_RESET_PASSWORD, msg);
		}
	}

	// }}

	// {{ callback
	@Override
	public void onRequestCompleted(String results) {
		if (results != null) {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder;
			try {
				builder = factory.newDocumentBuilder();
				Document document = builder.parse(new InputSource(
						new StringReader(results)));

				NodeList listOfItems = document.getElementsByTagName("error");
				int totalElements = listOfItems.getLength();

				if (totalElements > 0) {
					Element error = (Element) listOfItems.item(0);
					String message = error.getElementsByTagName("errorDesc")
							.item(0).getTextContent();
					Utils.alert(this, message);
				} else {
					alertDialogBuilder = new AlertDialog.Builder(this);
					alertDialogBuilder
							.setMessage(R.string.password_reset)
							.setCancelable(false)
							.setPositiveButton(R.string.ok,
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											Intent intent = new Intent(
													ForgotPassword.this,
													Index.class);
											startActivity(intent);
										}
									});
					alert = alertDialogBuilder.create();
					alert.show();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	// }}
}
